package com.huluobo.a2112mvvmdouyindemo.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2112mvvmdouyindemo.MainActivity;
import com.huluobo.a2112mvvmdouyindemo.R;
import com.huluobo.a2112mvvmdouyindemo.databinding.ActivityLoginBinding;
import com.huluobo.a2112mvvmdouyindemo.register.RegisterActivity;
import com.huluobo.mvvm_base.bean.Money;
import com.huluobo.mvvm_base.bean.UserBeanResult;
import com.huluobo.mvvm_base.db.AppDBUtils;
import com.huluobo.mvvm_base.net.Const;
import com.huluobo.mvvm_base.view.BaseMVVMActivity;
import com.hyphenate.EMCallBack;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;

import java.util.List;

public class LoginActivity extends BaseMVVMActivity<ActivityLoginBinding, LoginViewModel> {

    @Override
    protected void initData() {
        //观察ViewModel中Livedata的成功和失败
        viewModel.success.observe(this, userBeanResult -> {
            if (userBeanResult.getCode() == 200) {
                //登录成功保存token到SP中
                SPUtils.getInstance().put(Const.SP_TOKEN, userBeanResult.getData().getToken());
                SPUtils.getInstance().put(Const.SP_USERNAME, userBeanResult.getData().getUsername());
                //登录成功保存用户信息到数据库中
                AppDBUtils.getInstance().getUserDao().insertUserInfo(userBeanResult.getData());
                //登录时给用户一些钱
                Money money = new Money(userBeanResult.getData().getUsername(), 10000f);
                AppDBUtils.getInstance().getMoneyDao().insertUserMoney(money);
                EMClient.getInstance().login(userBeanResult.getData().getUsername(),
                        userBeanResult.getData().getPassword(), new EMCallBack() {
                            @Override
                            public void onSuccess() {
                                ToastUtils.showLong("欢迎回来:" + userBeanResult.getData().getUsername());
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }

                            @Override
                            public void onError(int code, String error) {
                                ToastUtils.showLong(error);
                            }
                        });


            } else {
                //非200的服务器失败
                ToastUtils.showLong(userBeanResult.getMessage());
            }
        });

        viewModel.failed.observe(this, s -> {
            ToastUtils.showLong("登录失败:" + s);
        });
    }

    @Override
    protected void initView() {
        dataBinding.loginToLogin.setOnClickListener(v -> {
            login();
        });

        dataBinding.loginToRegister.setOnClickListener(v -> {
            Intent intent = new Intent(this, RegisterActivity.class);
            startActivity(intent);
        });

        dataBinding.loginLogout.setOnClickListener(v -> {
            EMClient.getInstance().logout(true);
        });
    }

    //登录的方法
    private void login() {
        String usernameString = dataBinding.loginUsername.getText().toString();
        String passwordString = dataBinding.loginPassword.getText().toString();
        if (TextUtils.isEmpty(usernameString) || TextUtils.isEmpty(passwordString)) {
            ToastUtils.showLong("请输入用户名和密码");
            return;
        }
        //验证了用户名密码不为空
        viewModel.login(usernameString, passwordString);
    }

    @Override
    protected Class<LoginViewModel> getViewModel() {
        return LoginViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }
}